local M = _G.ClientOHOH.class.new(script,_G.ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name)
local ClientOHOH = _G.ClientOHOH
local RollGui = ClientOHOH.load.UIManager():CreateUI("RollGui",require(script.RollGui))
local CallBackManager = ClientOHOH.load.CallBackManager();
local autoConnection = nil 
local fastConnection = nil
local cd = nil
local isBuyFast = false
local ConnectClass = _G.CommonOHOH.load.Connect();
local game_cfg = ClientOHOH.config.Game
local gid = 1 --fastRoll gid
local Good_Cfg=ClientOHOH.config.Goods
local fastRollName = nil
local UIManager = ClientOHOH.load.UIManager();
local FunctionList = ClientOHOH.load.FunctionList();
local CarTool=_G.CommonOHOH.load.CarTool();
function M.new(self,...)
    return M(self,...)
end

function M.ctor(self,parent)
    M.super.ctor(self,parent)
    self.DataStore = self.parent:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())
    self.player = parent
    self.ShowUpper = 30 --显示权重在此之上的部件
    self:Init()
    self:AddConnect()
    RollGui:Show()
    self.Rng:CloseOrOpenAuto(false)
end

function M.Init(self)
    self.Rng=self.parent:GetComponent(_G.CommonOHOH.load.Common_Rng())
    self.RngDataLayer=self.parent:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())
    cd = self.RngDataLayer:GetRollCd()
    self.RollIsActivated = false
    self.AutoISActivated = false
    self.FastIsActivated = false
    self.RollIsOverEvent = ConnectClass.new()
    self.ClickAutoWhileRoll = false --是否正在单击roll时点击了auto
    for name, _ in pairs(Good_Cfg[gid].itemData) do
        fastRollName = name
    end
    local gamepassList = self.DataStore:GetData("gamePass")
    for index, _ in pairs(gamepassList) do
        if index == fastRollName then
            isBuyFast = true
        end
    end
end


function M.AddConnect(self)
    --result格式 是一个table，可能有多个{"10001_1","10002_2"}
    self.player.RollResultEvent:Connect(function(results)
        self:AddProcess(function()
        for _, carId in pairs(results) do
            if self.ShowUpper<CarTool.GetPartCfgData(carId).weight then
                RollGui:CreateCell(carId,self.DataStore,cd)
                self:Wait(0.1)
            end
        end
    end)

    end)

  

    --监听是否购买了fast_roll
    if not isBuyFast then
        local connection = nil
        connection = self.player.UserDataEvent:Connect(function(data)
            if data["gamePass"] then
                for index, _ in pairs(data["gamePass"]) do
                    if index == fastRollName then
                        isBuyFast = true
                        connection:Disconnect()
                    end
                end
            end
            
        end)
    end
  

    --roll按钮
    CallBackManager:AddRbxConnect(RollGui,RollGui.insts.rollBtn.Activated,function()
        --还在cd时间无操作
        if self.RollIsActivated==true or self.AutoISActivated then
            return
        else
            self.RollIsActivated = true
            self.Rng:OneRoll()
            RollGui:RollClick(cd)
            self:AddProcess(function()
                self:Wait(cd)
                self.RollIsActivated = false
                if self.ClickAutoWhileRoll then
                    self.ClickAutoWhileRoll = false
                    self.RollIsOverEvent:Action()
                end
            end)
        end
    end)

    --autoRoll按钮
    CallBackManager:AddRbxConnect(RollGui,RollGui.insts.autoBtn.Activated,function()
        self.AutoISActivated = not self.AutoISActivated
        if self.AutoISActivated and self.RollIsActivated then
            self.ClickAutoWhileRoll = true
            local connect = nil
            connect = self.RollIsOverEvent:Connect(function()
                connect:Disconnect()
                self:AutoRoll()
            end)
        elseif self.AutoISActivated and self.RollIsActivated==false then
            self:AutoRoll()
        else
            if autoConnection then
                autoConnection:Disconnect()
            end
            self.Rng:CloseOrOpenAuto(self.AutoISActivated)
        end
        RollGui:AutoRollClick(self.AutoISActivated)
    end)

    --fast按钮
    CallBackManager:AddRbxConnect(RollGui,RollGui.insts.fastBtn.Activated,function()
        if isBuyFast then
                self.FastIsActivated = not self.FastIsActivated
                if not self.RngDataCtrl then
                    self.RngDataCtrl = self.player:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())
                end
                if self.FastIsActivated then
                    self.RngDataCtrl:AddRollSpeed(game_cfg.fastRoll)
                else
                    self.RngDataCtrl:AddRollSpeed(-game_cfg.fastRoll)
                end
                self:Invoke(FunctionList.add_roll_speed,self.FastIsActivated) 
            RollGui:FastRollClick(self.FastIsActivated)
        else
            self.player.RobuxBuyEvent:Action(gid)
        end
    end)

    self.player.RollResultEvent:Connect(function()
        cd = self.RngDataLayer:GetRollCd()
    end)

end

--自动roll
function M.AutoRoll(self)
    self.Rng:CloseOrOpenAuto(self.AutoISActivated)
    autoConnection = self:AddProcess(function()
        while true do
            RollGui:RollClick(cd)
            self:Wait(cd)
        end
    end)
end

--修改显示权重
function M.SetShowWeight(self,weight)
    self.ShowUpper = weight
end


function M.dtor(self)
    M.super.dtor(self)
end

return M 